package com.fr.dialog.core;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.NameStyle;
import com.fr.base.Style;
import com.fr.base.core.StyleUtils;
import com.fr.cell.GridSelection;
import com.fr.cell.ReportPane;
import com.fr.dialog.NameObject;
import com.fr.dialog.UpdatePane;
import com.fr.report.CellElement;
import com.fr.report.DefaultCellElement;
import com.fr.report.FloatElement;
import com.fr.report.Report;
import com.fr.report.TemplateReport;
import com.fr.report.core.PaintUtils;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class StylePane extends UpdatePane
  implements ChangeListener
{
  private ReportPane reportPane;
  protected Style oldStyle;
  private NameStyle globalStyle;
  private FormatPane formatPane = null;
  private AlignmentPane alignmentPane = null;
  private FRFontPane frFontPane = null;
  private BorderPane borderPane = null;
  private BackgroundPane backgroundPane = null;
  private PreivewArea previewArea;
  private JPanel previewPane;
  protected NameObject nameObject;
  protected ChangeListener tabChangeActionListener = new ChangeListener(this)
  {
    private final StylePane this$0;

    public void stateChanged()
    {
      Object localObject = paramChangeEvent.getSource();
      if ((localObject == null) || (!(localObject instanceof JTabbedPane)))
        return;
      JTabbedPane localJTabbedPane = (JTabbedPane)localObject;
      int i = localJTabbedPane.getSelectedIndex();
      if (localJTabbedPane.getComponentAt(i).getClass() == ((StylePane.class$javax$swing$JPanel == null) ? (StylePane.class$javax$swing$JPanel = StylePane.class$("javax.swing.JPanel")) : StylePane.class$javax$swing$JPanel))
        if (i == 1)
          localJTabbedPane.setComponentAt(i, StylePane.access$000(this.this$0));
        else if (i == 2)
          localJTabbedPane.setComponentAt(i, StylePane.access$100(this.this$0));
        else if (i == 3)
          localJTabbedPane.setComponentAt(i, StylePane.access$200(this.this$0));
        else if (i == 4)
          localJTabbedPane.setComponentAt(i, StylePane.access$300(this.this$0));
      this.this$0.updatePreviewArea();
    }
  };
  static Class class$javax$swing$JPanel;

  public StylePane()
  {
    setLayout(new BorderLayout());
    this.previewPane = new JPanel(new BorderLayout());
    add(this.previewPane, "North");
    this.previewPane.setBorder(BorderFactory.createTitledBorder(Inter.getLocText("Preview") + ":"));
    this.previewArea = new PreivewArea();
    this.previewPane.add(this.previewArea, "Center");
    JPanel localJPanel = new JPanel(new GridLayout(2, 1, 2, 2));
    this.previewPane.add(localJPanel, "East");
    JTabbedPane localJTabbedPane = new JTabbedPane();
    add(localJTabbedPane, "Center");
    localJTabbedPane.addTab(Inter.getLocText("Format"), getFormatPane());
    localJTabbedPane.addTab(Inter.getLocText("Alignment"), new JPanel());
    localJTabbedPane.addTab(Inter.getLocText("Sytle-FRFont"), new JPanel());
    localJTabbedPane.addTab(Inter.getLocText("Border"), new JPanel());
    localJTabbedPane.addTab(Inter.getLocText("Background"), new JPanel());
    localJTabbedPane.addChangeListener(this.tabChangeActionListener);
  }

  public void stateChanged(ChangeEvent paramChangeEvent)
  {
    updatePreviewArea();
  }

  protected FormatPane getFormatPane()
  {
    if (this.formatPane == null)
    {
      this.formatPane = new FormatPane();
      if (this.oldStyle != null)
        this.formatPane.populate(this.oldStyle.getFormat());
    }
    return this.formatPane;
  }

  private AlignmentPane getAlignmentPane()
  {
    if (this.alignmentPane == null)
    {
      this.alignmentPane = new AlignmentPane();
      this.alignmentPane.addChangeListener(this);
      if (this.oldStyle != null)
        this.alignmentPane.populate(this.oldStyle);
    }
    return this.alignmentPane;
  }

  private FRFontPane getFRFontPane()
  {
    if (this.frFontPane == null)
    {
      this.frFontPane = new FRFontPane();
      this.frFontPane.addChangeListener(this);
      if (this.oldStyle != null)
        this.frFontPane.populate(this.oldStyle.getFRFont());
    }
    return this.frFontPane;
  }

  private BorderPane getBorderPane()
  {
    if (this.borderPane == null)
    {
      this.borderPane = new BorderPane();
      this.borderPane.addChangeListener(this);
      if (this.reportPane != null)
        this.borderPane.populate(this.reportPane);
      else if (this.oldStyle != null)
        this.borderPane.populate(this.oldStyle);
    }
    return this.borderPane;
  }

  private BackgroundPane getBackgroundPane()
  {
    if (this.backgroundPane == null)
    {
      this.backgroundPane = new BackgroundPane();
      this.backgroundPane.addChangeListener(this);
      if (this.oldStyle != null)
        this.backgroundPane.populate(this.oldStyle.getBackground());
    }
    return this.backgroundPane;
  }

  public NameStyle getGlobalStyle()
  {
    return this.globalStyle;
  }

  public void setGlobalStyle(NameStyle paramNameStyle)
  {
    this.globalStyle = paramNameStyle;
  }

  public void populate(ReportPane paramReportPane)
  {
    this.reportPane = paramReportPane;
    this.nameObject = new NameObject("", analyzeCurrentStyle(paramReportPane));
    populate(this.nameObject);
    updatePreviewArea();
  }

  private Style analyzeCurrentStyle(ReportPane paramReportPane)
  {
    Object localObject1;
    Object localObject2;
    Style localStyle = null;
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    int i = localGridSelection.getType();
    if (i == 1)
    {
      localObject1 = paramReportPane.getEditingReport();
      localObject2 = ((Report)localObject1).getFloatElement(localGridSelection.getFloatName(0));
      localStyle = ((FloatElement)localObject2).getStyle();
    }
    else
    {
      localObject1 = localGridSelection.getEditRectangle();
      localObject2 = paramReportPane.getEditingReport();
      CellElement localCellElement = ((Report)localObject2).getCellElement(((Rectangle)localObject1).x, ((Rectangle)localObject1).y);
      if ((localCellElement != null) && (localCellElement.getStyle() != null))
        try
        {
          localStyle = (Style)localCellElement.getStyle().clone();
        }
        catch (CloneNotSupportedException localCloneNotSupportedException)
        {
          FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
        }
    }
    if (localStyle == null)
      localStyle = Style.DEFAULT_STYLE;
    return ((Style)(Style)localStyle);
  }

  public boolean updateGlobalStyle(ReportPane paramReportPane)
  {
    Object localObject2;
    updatePreviewArea();
    NameStyle localNameStyle = getGlobalStyle();
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    int i = localGridSelection.getType();
    if (i == 1)
    {
      int j = localGridSelection.getFloatNameCount();
      k = 0;
      while (true)
      {
        if (k >= j)
          break label217;
        java.lang.String str = localGridSelection.getFloatName(k);
        localObject2 = localTemplateReport.getFloatElement(str);
        ((FloatElement)localObject2).setStyle(localNameStyle);
        ++k;
      }
    }
    int k = localGridSelection.getCellRectangleCount();
    for (int l = 0; l < k; ++l)
    {
      localObject2 = localGridSelection.getCellRectangle(l);
      for (int i1 = 0; i1 < ((Rectangle)localObject2).height; ++i1)
        for (int i2 = 0; i2 < ((Rectangle)localObject2).width; ++i2)
        {
          int i3 = i2 + ((Rectangle)localObject2).x;
          int i4 = i1 + ((Rectangle)localObject2).y;
          Object localObject1 = localTemplateReport.getCellElement(i3, i4);
          if (localObject1 == null)
          {
            localObject1 = new DefaultCellElement(i3, i4);
            localTemplateReport.addCellElement((CellElement)localObject1);
          }
          ((CellElement)localObject1).setStyle(localNameStyle);
        }
    }
    if (this.borderPane != null)
      label217: this.borderPane.update(paramReportPane);
    return true;
  }

  public boolean update(ReportPane paramReportPane)
  {
    Object localObject2;
    Style localStyle1 = analyzeCurrentStyle(paramReportPane);
    try
    {
      localStyle2 = (Style)localStyle1.clone();
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
      return false;
    }
    if (this.nameObject == null)
      return false;
    update();
    Style localStyle2 = (Style)this.nameObject.getObject();
    TemplateReport localTemplateReport = paramReportPane.getEditingReport();
    GridSelection localGridSelection = paramReportPane.getGridSelection();
    int i = localGridSelection.getType();
    if (i == 1)
    {
      int j = localGridSelection.getFloatNameCount();
      k = 0;
      while (true)
      {
        if (k >= j)
          break label293;
        java.lang.String str = localGridSelection.getFloatName(k);
        localObject2 = localTemplateReport.getFloatElement(str);
        ((FloatElement)localObject2).setStyle(StyleUtils.applyCellStyle(localStyle1, localStyle2, ((FloatElement)localObject2).getStyle()));
        ++k;
      }
    }
    int k = localGridSelection.getCellRectangleCount();
    for (int l = 0; l < k; ++l)
    {
      localObject2 = localGridSelection.getCellRectangle(l);
      for (int i1 = 0; i1 < ((Rectangle)localObject2).height; ++i1)
        for (int i2 = 0; i2 < ((Rectangle)localObject2).width; ++i2)
        {
          int i3 = i2 + ((Rectangle)localObject2).x;
          int i4 = i1 + ((Rectangle)localObject2).y;
          Object localObject1 = localTemplateReport.getCellElement(i3, i4);
          if (localObject1 == null)
          {
            localObject1 = new DefaultCellElement(i3, i4);
            localTemplateReport.addCellElement((CellElement)localObject1);
          }
          ((CellElement)localObject1).setStyle(StyleUtils.applyCellStyle(localStyle1, localStyle2, ((CellElement)localObject1).getStyle()));
        }
    }
    if (this.borderPane != null)
      label293: this.borderPane.update(paramReportPane);
    paramReportPane.repaint();
    return true;
  }

  public void populate(Object paramObject)
  {
    if ((paramObject == null) || (!(paramObject instanceof NameObject)))
      return;
    this.nameObject = ((NameObject)paramObject);
    Style localStyle = (Style)this.nameObject.getObject();
    this.oldStyle = localStyle;
    if (this.formatPane != null)
      this.formatPane.populate(this.oldStyle.getFormat());
    if (this.alignmentPane != null)
      this.alignmentPane.populate(this.oldStyle);
    if (this.frFontPane != null)
      this.frFontPane.populate(this.oldStyle.getFRFont());
    if (this.borderPane != null)
      this.borderPane.populate(this.oldStyle);
    if (this.backgroundPane != null)
      this.backgroundPane.populate(this.oldStyle.getBackground());
    updatePreviewArea();
  }

  public void update()
  {
    if (this.nameObject == null)
      return;
    try
    {
      if (this.alignmentPane != null)
        this.alignmentPane.checkValid();
    }
    catch (Exception localException)
    {
      JOptionPane.showMessageDialog(this, localException.getMessage());
      return;
    }
    Style localStyle = (Style)this.nameObject.getObject();
    if (this.formatPane != null)
      localStyle = localStyle.deriveFormat(this.formatPane.update());
    if (this.alignmentPane != null)
      localStyle = this.alignmentPane.update(localStyle);
    if (this.frFontPane != null)
      localStyle = localStyle.deriveFRFont(this.frFontPane.update());
    if (this.frFontPane != null)
      localStyle = localStyle.deriveFRFont(this.frFontPane.update());
    if (this.borderPane != null)
      localStyle = this.borderPane.update(localStyle);
    if (this.backgroundPane != null)
      localStyle = localStyle.deriveBackground(this.backgroundPane.update());
    this.nameObject.setObject(localStyle);
  }

  public void updatePreviewArea()
  {
    if (this.oldStyle != null)
    {
      if (this.nameObject == null)
        return;
      update();
      this.previewArea.setStyle((Style)this.nameObject.getObject());
    }
  }

  public PreivewArea getPreviewArea()
  {
    return this.previewArea;
  }

  static Class class$(java.lang.String paramString)
  {
    try
    {
      return Class.forName(paramString);
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
    }
  }

  static AlignmentPane access$000(StylePane paramStylePane)
  {
    return paramStylePane.getAlignmentPane();
  }

  static FRFontPane access$100(StylePane paramStylePane)
  {
    return paramStylePane.getFRFontPane();
  }

  static BorderPane access$200(StylePane paramStylePane)
  {
    return paramStylePane.getBorderPane();
  }

  static BackgroundPane access$300(StylePane paramStylePane)
  {
    return paramStylePane.getBackgroundPane();
  }

  public static class PreivewArea extends JComponent
  {
    private java.lang.String paintText = "Report";
    private Style style = Style.DEFAULT_STYLE;

    public PreivewArea()
    {
      setPreferredSize(new Dimension(40, 40));
    }

    public void setStyle(Style paramStyle)
    {
      this.style = paramStyle;
      repaint();
    }

    public void paintComponent(Graphics paramGraphics)
    {
      Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
      if (this.style == Style.DEFAULT_STYLE)
      {
        PaintUtils.paintContent(localGraphics2D, this.paintText, this.style, getWidth() - 3, getHeight() - 3);
        return;
      }
      PaintUtils.paintBackground(localGraphics2D, this.style, getWidth() - 3, getHeight() - 3);
      PaintUtils.paintContent(localGraphics2D, this.paintText, this.style, getWidth() - 3, getHeight() - 3);
      PaintUtils.paintBorder(localGraphics2D, this.style, getWidth() - 3, getHeight() - 3);
    }

    public Dimension getMinimumSize()
    {
      return getPreferredSize();
    }
  }

  class FRFontListSelectionListener
    implements ListSelectionListener
  {
    private final StylePane this$0;

    FRFontListSelectionListener()
    {
      this.this$0 = paramStylePane;
    }

    public void valueChanged()
    {
      this.this$0.updatePreviewArea();
    }
  }
}